/*******************************************************************************
 * Copyright (c) 2014 Steffen Mikkelsen
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Steffen Mikkelsen - initial API and implementation
 ******************************************************************************/
package com.subsecret.secureconnection.securestream.layers;

import java.util.HashMap;

import com.subsecret.secureconnection.securestream.SecureStreamLayer;
import com.subsecret.secureconnection.securestream.layers.aes.AESEncryptionLayer;
import com.subsecret.secureconnection.securestream.layers.gzip.GZIPCompressionLayer;
import com.subsecret.secureconnection.securestream.layers.obfuscation.ObfuscationLayer;

public class StreamLayers {
    
    private static HashMap<Integer, Class<? extends SecureStreamLayer>> layers = new HashMap<>();
    
    static {
        registerLayer(GZIPCompressionLayer.class);
        registerLayer(ObfuscationLayer.class);
        registerLayer(AESEncryptionLayer.class);
    }
    
    public static SecureStreamLayer getLayer(int id) {
        try {
            if (layers.containsKey(id)) {
                return (SecureStreamLayer) layers.get(id).newInstance();
            }
            else {
                throw new RuntimeException("No layer with ID: " + id);
            }
        }
        catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static void registerLayer(Class<? extends SecureStreamLayer> layerClass) {
        try {
            layers.put(layerClass.newInstance().getLayerID(), layerClass);
        }
        catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
